<?php
include __DIR__ . '/../autoload.php';
use lib\LocalRedis;
use lib\Mysql;
go(function () {
    $mysql = new Mysql();
    $localRedis = new LocalRedis();
    
    $time = time();
    
    for($i = 0; $i < 1000; $i ++){
        
        $data = $localRedis->rpop('heart-list');
        if($data === null) {
            break;
        }
        
        $data = json_decode($data, true);
        
        $simId = $data['deviceId'];
        $deviceId = $localRedis->get($simId);
        if(!$deviceId) {
            $deviceId = $mysql->value('device', "sim_iccid = '{$simId}'", 'device_id');
            if($deviceId) {
                $localRedis->set($simId, $deviceId);
            }
        }
        if(!$deviceId) {
            continue;
        }
        var_dump($deviceId);
        $chargePort = 0;
        $ports = json_decode($data['ports'], true);
        foreach ($ports as $k => $port) {
            if($port['stt1'] == 1) {
                $chargePort ++;
            }
            $portNo = $k + 1;
            $mysql->update('device_port', "device_id = {$deviceId} and port_no = {$portNo}", ['status' => $port['stt1'], 'surplus_time' => $port['stt2'], 'surplus_energy' => round($port['stt3'] / 1000, 3), 'power' => $port['stt4']]);
            var_dump($mysql->lastSql);
        }
        
        $mysql->update('device', "device_id = {$deviceId}", ['heart_time' => $time, 'charge_port' => $chargePort, 'lock_status' => $data['lock_status'], 'signal' => $data['signal']]);
    }
    
    $localRedis->close();
    $mysql->close();
    
});